package com.unpor.controller.g;

import com.jfinal.core.Controller;
import com.unpor.common.StaticFinalVar;
import com.unpor.model.g.GAdmin;
import com.unpor.model.g.GGroupLimit;
import com.unpor.util.MD5Util;
import org.apache.commons.collections.map.HashedMap;

import java.util.List;
import java.util.Map;


/**
 * 用户登录
 * Created by benhailong on 2017/11/5.
 */
public class GAdminController extends Controller {
    public void index(){
        setAttr("title", StaticFinalVar.TITLE);
        renderTemplate("login.html");
    }



















    /**
     * 后台登录
     */
    public void tologin(){
        Map<String,Object> map = new HashedMap();
        // 获取参数
        String username = getPara("username");
        String password = getPara("password");
        if(username==null || "".equals(username)){
            map.put("code","101");
            map.put("msg","用户名不能为空");
            renderJson(map);
        }
        if(username==null || "".equals(username)){
            map.put("code","101");
            map.put("msg","密码不能为空");
            renderJson(map);
        }

        // 登录 的  sql
        String sql = GAdmin.dao.getSql("gAdmin.tologin");

        GAdmin gAdmin = GAdmin.dao.findFirst(sql, username, MD5Util.MD5(password));
        if(gAdmin==null){
            map.put("code","101");
            map.put("msg","登录失败");
            renderJson(map);
        }

        String groupname = gAdmin.get("group_name");

//        String sql2 = GAdmin.dao.getSql("gAdmin.groupAndLimit");
//        String groupId = gAdmin.get("group_id");

//        List<GGroupLimit> gGroupLimits = GGroupLimit.dao.find(sql2, groupId);

        // 调用服务器信息
        ServiceInformation();


//        setSessionAttr("groupList",gGroupLimits);
        if(gAdmin!=null){
            map.put("admin",gAdmin);
            map.put("code","200");
            map.put("msg","登录成功");
            map.put("url","/admin/adminindex");
        }
        renderJson(map);
    }

    /**
     * 跳转到后台首页
     */
    public void adminindex(){
        System.out.println("来了》》》》》》》》》");
        ServiceInformation();
        render("g/index.html");
    }



    // 获取服务器端的基本信息
    private void ServiceInformation() {
        // 获取服务器信息
        String serverName = getRequest().getServerName();
        String serverPort = String.valueOf(getRequest().getServerPort());
        String servletPath = getRequest().getServletPath();
        String servletContext = String.valueOf(getRequest().getServletContext());
        setAttr("serverName",serverName);
        setAttr("serverPort",serverPort);
        setAttr("servletPath",servletPath);
        setAttr("servletContext",servletContext);
    }
}
